package com.dongjiantong.farmrecords.serviceImpl;

import com.alibaba.dubbo.config.annotation.Service;
import com.dongjiantong.common.exceptions.HttpCode;
import com.dongjiantong.common.exceptions.OPServerException;
import com.dongjiantong.farmrecords.enums.FarmRecordsNumberStatus;
import com.dongjiantong.farmrecords.mapper.FarmRecordsImmunizationMapper;
import com.dongjiantong.farmrecords.mapper.FarmRecordsImmunizationSubMapper;
import com.dongjiantong.farmrecords.model.FarmRecordsImmunization;
import com.dongjiantong.farmrecords.model.FarmRecordsImmunizationSub;
import com.dongjiantong.farmrecords.service.FarmRecordsImmunizationSubService;
import com.dongjiantong.mybatis.ServiceMybatis;
import lombok.extern.slf4j.Slf4j;

import javax.annotation.Resource;
import java.util.*;

@Slf4j
@Service(version = "1.0.0")
public class FarmRecordsImmunizationSubServiceImpl extends ServiceMybatis<FarmRecordsImmunizationSub> implements FarmRecordsImmunizationSubService {

    @Resource
    private FarmRecordsImmunizationSubMapper farmRecordsImmunizationSubMapper;
    @Resource
    private FarmRecordsImmunizationMapper farmRecordsImmunizationMapper;

    /**
     * 根据免疫子表id 查询一条免疫记录信息(即 首免或者二免,三免之类)
     * @param id
     * @return Response<FarmRecordsImmunizationSub>
     */
    @Override
    public FarmRecordsImmunizationSub selectBySubId(Long id) {
        try {
            if (farmRecordsImmunizationSubMapper == null){
                throw new OPServerException("farmRecordsImmunizationSubMapper 为null,请检查");
            }
            return farmRecordsImmunizationSubMapper.selectBySubId(id);
        } catch (Exception e) {
            log.error(e.getMessage());
            throw new OPServerException(HttpCode.INTERNAL_SERVER_ERROR,HttpCode.INTERNAL_SERVER_ERROR.getMessage());
        }
    }

    /**
     * 新建一条免疫记录 (除首免之外的免疫)
     * @return long 插入数据的id
     */
    @Override
    public Long createOneSub(FarmRecordsImmunization farmRecordsImmunization, FarmRecordsImmunizationSub farmRecordsImmunizationSub, Integer times) {
        try {
            if (farmRecordsImmunizationSubMapper == null){
                throw new OPServerException("farmRecordsImmunizationSubMapper 为null,请检查");
            }
            if (farmRecordsImmunizationMapper == null){
                throw new OPServerException("farmRecordsImmunizationMapper 为null,请检查");
            }
            if (times == null){
                throw new OPServerException(HttpCode.BAD_REQUEST,"请求的免疫期次记录数不能为空");
            }
            int id = 0;//子表记录的id
            if (times != 1) {
                farmRecordsImmunizationSub.setVaccine_id(farmRecordsImmunization.getId());
                id = farmRecordsImmunizationSubMapper.insert(farmRecordsImmunizationSub);

                if (id != 0) {
                    //新创建的数据成功后更新免疫记录主体的更新日期
                    farmRecordsImmunization.setUpdated_at(new Date());
                    farmRecordsImmunizationMapper.updateByPrimaryKeySelective(farmRecordsImmunization);
                }
            }
            return Long.valueOf(id);
        } catch (OPServerException e) {
            log.error(e.getMessage());
            throw new OPServerException(HttpCode.INTERNAL_SERVER_ERROR,HttpCode.INTERNAL_SERVER_ERROR.getMessage());
        }
    }

    /**
     * 更新免疫记录子表对应信息的单据可用与否
     * @param vaccineId
     */
    @Override
    public Integer updateDeleteFlag(Long vaccineId) {
        Integer flag = 0;
        try {
            if (farmRecordsImmunizationSubMapper == null){
                throw new OPServerException("farmRecordsImmunizationSubMapper 为null,请检查");
            }
            Map map = new HashMap();
            map.put("vaccine_id",vaccineId);
            map.put("delete_flag", FarmRecordsNumberStatus.INVALID.getCode());
            flag = farmRecordsImmunizationSubMapper.updateDeleteFlag(map);
            return flag;
        } catch (OPServerException e) {
            log.error(e.getMessage());
            throw new OPServerException(HttpCode.INTERNAL_SERVER_ERROR,HttpCode.INTERNAL_SERVER_ERROR.getMessage());
        }
    }
}